﻿namespace HuaWei.CommComp
{
    using DevExpress.XtraEditors;
    using DevExpress.XtraEditors.Controls;
    using HuaWei.CommClass;
    using HuaWei.Common;
    using HuaWei.DAI;
    using HuaWei.Windows.Forms;
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;

    public class FrmAddQc : Form
    {
        private bool _flag;
        private DataTable dataTable = null;
        public DateEdit edtJSRQ;
        public DateEdit edtQQRQ;
        private IContainer icontainer = null;
        private LabelControl labelControl1;
        private LabelControl labelControl2;
        private SimpleButton simpleButton1;
        private LabelControl labelControl3;
        private TextEdit edtZFBH;
        private TextEdit edtZFBHForThree;
        private TextEdit edtZFBHForFour;
        private LabelControl labelControl4;
        private LabelControl labelControl5;
        private SimpleButton simpleButton2;

        public FrmAddQc()
        {
            this.InitializeComponent();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.icontainer != null))
            {
                this.icontainer.Dispose();
            }
            base.Dispose(disposing);
        }

        private void SaveData()
        {
            IDataAccess access = DBHelp.IniIDA();
            access.Update(this.dataTable);
            access.CloseDatabase();
        }

        public void IniQC(bool isAdd)
        {
            this.dataTable = GParams._QCData;
            this._flag = isAdd;
            if (!isAdd)
            {
                this.Text = "当前为" + GParams.QCInfo.QCID + "期";
                this.edtQQRQ.EditValue = GParams.QCInfo.QQRQ;
                this.edtJSRQ.EditValue = GParams.QCInfo.JSRQ;
                DataRow[] rowArray = this.dataTable.Select("qcid=" + GParams.QCInfo.QCID);
                if (rowArray.Length > 0)
                {
                    this.edtZFBH.EditValue = rowArray[0]["ZFBH"];
                    this.edtZFBHForThree.EditValue = rowArray[0]["ZFBHForThree"];
                    this.edtZFBHForFour.EditValue = rowArray[0]["ZFBHForFour"];
                }
            }
            else
            {
                DataRow[] rowArray = this.dataTable.Select("qcid=max(QCID)");
                if (rowArray.Length > 0)
                {
                    this.edtQQRQ.EditValue = Convert.ToDateTime(rowArray[0]["JSRQ"]).AddDays(1.0);
                    this.edtJSRQ.EditValue = Convert.ToDateTime(rowArray[0]["JSRQ"]).AddMonths(1);
                }
            }
        }

        private void InitializeComponent()
        {
            this.edtQQRQ = new DevExpress.XtraEditors.DateEdit();
            this.edtJSRQ = new DevExpress.XtraEditors.DateEdit();
            this.labelControl1 = new DevExpress.XtraEditors.LabelControl();
            this.labelControl2 = new DevExpress.XtraEditors.LabelControl();
            this.simpleButton1 = new DevExpress.XtraEditors.SimpleButton();
            this.simpleButton2 = new DevExpress.XtraEditors.SimpleButton();
            this.labelControl3 = new DevExpress.XtraEditors.LabelControl();
            this.edtZFBH = new DevExpress.XtraEditors.TextEdit();
            this.edtZFBHForThree = new DevExpress.XtraEditors.TextEdit();
            this.edtZFBHForFour = new DevExpress.XtraEditors.TextEdit();
            this.labelControl4 = new DevExpress.XtraEditors.LabelControl();
            this.labelControl5 = new DevExpress.XtraEditors.LabelControl();
            ((System.ComponentModel.ISupportInitialize)(this.edtQQRQ.Properties.VistaTimeProperties)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.edtQQRQ.Properties)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.edtJSRQ.Properties.VistaTimeProperties)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.edtJSRQ.Properties)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.edtZFBH.Properties)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.edtZFBHForThree.Properties)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.edtZFBHForFour.Properties)).BeginInit();
            this.SuspendLayout();
            // 
            // edtQQRQ
            // 
            this.edtQQRQ.EditValue = null;
            this.edtQQRQ.Location = new System.Drawing.Point(103, 19);
            this.edtQQRQ.Name = "edtQQRQ";
            this.edtQQRQ.Properties.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {
            new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)});
            this.edtQQRQ.Properties.VistaTimeProperties.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {
            new DevExpress.XtraEditors.Controls.EditorButton()});
            this.edtQQRQ.Size = new System.Drawing.Size(188, 21);
            this.edtQQRQ.TabIndex = 0;
            // 
            // edtJSRQ
            // 
            this.edtJSRQ.EditValue = null;
            this.edtJSRQ.Location = new System.Drawing.Point(103, 58);
            this.edtJSRQ.Name = "edtJSRQ";
            this.edtJSRQ.Properties.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {
            new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)});
            this.edtJSRQ.Properties.VistaTimeProperties.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {
            new DevExpress.XtraEditors.Controls.EditorButton()});
            this.edtJSRQ.Size = new System.Drawing.Size(188, 21);
            this.edtJSRQ.TabIndex = 1;
            // 
            // labelControl1
            // 
            this.labelControl1.Location = new System.Drawing.Point(49, 22);
            this.labelControl1.Name = "labelControl1";
            this.labelControl1.Size = new System.Drawing.Size(48, 14);
            this.labelControl1.TabIndex = 2;
            this.labelControl1.Text = "起始日期";
            // 
            // labelControl2
            // 
            this.labelControl2.Location = new System.Drawing.Point(49, 61);
            this.labelControl2.Name = "labelControl2";
            this.labelControl2.Size = new System.Drawing.Size(48, 14);
            this.labelControl2.TabIndex = 3;
            this.labelControl2.Text = "结束日期";
            // 
            // simpleButton1
            // 
            this.simpleButton1.Location = new System.Drawing.Point(121, 205);
            this.simpleButton1.Name = "simpleButton1";
            this.simpleButton1.Size = new System.Drawing.Size(75, 23);
            this.simpleButton1.TabIndex = 4;
            this.simpleButton1.Text = "确定";
            this.simpleButton1.Click += new System.EventHandler(this.simpleButton1_Click);
            // 
            // simpleButton2
            // 
            this.simpleButton2.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.simpleButton2.Location = new System.Drawing.Point(216, 205);
            this.simpleButton2.Name = "simpleButton2";
            this.simpleButton2.Size = new System.Drawing.Size(75, 23);
            this.simpleButton2.TabIndex = 5;
            this.simpleButton2.Text = "取消";
            // 
            // labelControl3
            // 
            this.labelControl3.Location = new System.Drawing.Point(13, 101);
            this.labelControl3.Name = "labelControl3";
            this.labelControl3.Size = new System.Drawing.Size(84, 14);
            this.labelControl3.TabIndex = 6;
            this.labelControl3.Text = "支表一支付编号";
            // 
            // edtZFBH
            // 
            this.edtZFBH.Location = new System.Drawing.Point(103, 98);
            this.edtZFBH.Name = "edtZFBH";
            this.edtZFBH.Size = new System.Drawing.Size(188, 21);
            this.edtZFBH.TabIndex = 7;
            // 
            // edtZFBHForThree
            // 
            this.edtZFBHForThree.Location = new System.Drawing.Point(103, 137);
            this.edtZFBHForThree.Name = "edtZFBHForThree";
            this.edtZFBHForThree.Size = new System.Drawing.Size(189, 21);
            this.edtZFBHForThree.TabIndex = 9;

            // edtZFBHForFour
            // 
            this.edtZFBHForFour.Location = new System.Drawing.Point(103, 177);
            this.edtZFBHForFour.Name = "edtZFBHForFour";
            this.edtZFBHForFour.Size = new System.Drawing.Size(189, 21);
            this.edtZFBHForFour.TabIndex = 9;

            // 
            // labelControl5
            // 
            this.labelControl5.Location = new System.Drawing.Point(13, 177);
            this.labelControl5.Name = "labelControl5";
            this.labelControl5.Size = new System.Drawing.Size(84, 14);
            this.labelControl5.TabIndex = 8;
            this.labelControl5.Text = "支表四支付编号";

            // 
            // labelControl4
            // 
            this.labelControl4.Location = new System.Drawing.Point(13, 140);
            this.labelControl4.Name = "labelControl4";
            this.labelControl4.Size = new System.Drawing.Size(84, 14);
            this.labelControl4.TabIndex = 8;
            this.labelControl4.Text = "支表三支付编号";
            // 
            // FrmAddQc
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(319, 235);
            this.Controls.Add(this.edtZFBHForFour);
            this.Controls.Add(this.edtZFBHForThree);
            this.Controls.Add(this.labelControl4);
            this.Controls.Add(this.labelControl5);
            this.Controls.Add(this.edtZFBH);
            this.Controls.Add(this.labelControl3);
            this.Controls.Add(this.simpleButton2);
            this.Controls.Add(this.simpleButton1);
            this.Controls.Add(this.labelControl2);
            this.Controls.Add(this.labelControl1);
            this.Controls.Add(this.edtJSRQ);
            this.Controls.Add(this.edtQQRQ);
            this.Name = "FrmAddQc";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            this.Text = "计量期次";
            ((System.ComponentModel.ISupportInitialize)(this.edtQQRQ.Properties.VistaTimeProperties)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.edtQQRQ.Properties)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.edtJSRQ.Properties.VistaTimeProperties)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.edtJSRQ.Properties)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.edtZFBH.Properties)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.edtZFBHForThree.Properties)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.edtZFBHForFour.Properties)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void method()
        {
            string str = "";
            this.dataTable.TableNewRow += new DataTableNewRowEventHandler(Class28.smethod);

            DataTable dtTemp = this.dataTable.Copy();
            dtTemp.Columns.Add(new DataColumn("QCNum", typeof(int)));
            foreach (DataRow dr in dtTemp.Rows)
            {
                dr["QCNum"] = Convert.ToInt32(dr["QCID"]);
            }

            DataRow[] rowArray = dtTemp.Select("qcid=max(QCNum)");
            if (rowArray.Length > 0)
            {
                str = rowArray[0]["qcid"].ToString();
            }
            else
            {
                str = "0";
            }
            DataRow row = this.dataTable.NewRow();
            str = (Convert.ToInt16(str) + 1).ToString();
            row["qcid"] = str;
            row["zt"] = -1;
            row["QQRQ"] = this.edtQQRQ.EditValue;
            row["JSRQ"] = this.edtJSRQ.EditValue;
            row["ZFBH"] = this.edtZFBH.EditValue;
            row["ZFBHForThree"] = this.edtZFBHForThree.EditValue;
            row["ZFBHForFour"] = this.edtZFBHForFour.EditValue;
            row.EndEdit();
            this.dataTable.Rows.Add(row);
            this.SaveData();
            QCInfo info = new QCInfo();
            info.QCID = str;
            info.QCZT = -1;
            info.QQRQ = this.edtQQRQ.DateTime;
            info.JSRQ = this.edtJSRQ.DateTime;
            GParams.QCInfo = info;
        }

        private void method1()
        {
            QCInfo qCInfo = GParams.QCInfo;
            qCInfo.QQRQ = this.edtQQRQ.DateTime;
            qCInfo.JSRQ = this.edtJSRQ.DateTime;
            GParams.QCInfo = qCInfo;
            foreach (DataRow row in this.dataTable.Rows)
            {
                if (row["qcid"].Equals(qCInfo.QCID))
                {
                    row["QQRQ"] = this.edtQQRQ.EditValue;
                    row["JSRQ"] = this.edtJSRQ.EditValue;
                    row["ZFBH"] = this.edtZFBH.EditValue;
                    row["ZFBHForThree"] = this.edtZFBHForThree.EditValue;
                    row["ZFBHForFour"] = this.edtZFBHForFour.EditValue;
                    row.EndEdit();
                }
            }
            this.SaveData();
        }

        private bool method2()
        {
            int num = Convert.ToInt16(GParams.QCInfo.QCID);
            IEnumerator enumerator = this.dataTable.Rows.GetEnumerator();
            {
                DataRow current;
                while (enumerator.MoveNext())
                {
                    current = (DataRow) enumerator.Current;
                    DateTime time = Convert.ToDateTime(current["qqrq"]);
                    DateTime time2 = Convert.ToDateTime(current["jsrq"]);
                    if ((num > Convert.ToInt16(current["qcid"])) || this._flag)
                    {
                        if (DateTime.Compare(this.edtQQRQ.DateTime, time2) > 0)
                        {
                            continue;
                        }
                        goto Label_00CB;
                    }
                    if ((num < Convert.ToInt16(current["qcid"])) && (DateTime.Compare(this.edtJSRQ.DateTime, time) >= 0))
                    {
                        goto Label_00F0;
                    }
                }
                goto Label_012C;
            Label_00CB:
                MsgBox.ShowMsg(string.Format("起始日期位于第{0}期结束日期之前，请修正后继续!", current["qcid"].ToString()));
                return false;
            Label_00F0:
                MsgBox.ShowMsg(string.Format("结束日期位于第{0}期起始日期之后，请修正后继续!", current["qcid"].ToString()));
                return false;
            }
        Label_012C:
            return true;
        }

        private bool method3()
        {
            if (DateTime.Compare(this.edtQQRQ.DateTime, this.edtJSRQ.DateTime) >= 0)
            {
                MsgBox.ShowMsg("结束日期不能大于或等于起启日期，请修正后继续!");
                return false;
            }
            if (this.edtJSRQ.Text.Equals("") && this.edtQQRQ.Text.Equals(""))
            {
                MsgBox.ShowMsg("起启日期与结束日期不能为空，请修正后继续!");
                return false;
            }
            if (string.IsNullOrEmpty(this.edtZFBH.EditValue as string) || string.IsNullOrEmpty(this.edtZFBHForThree.EditValue as string)|| string.IsNullOrEmpty(this.edtZFBHForFour.EditValue as string))
            {
                MsgBox.ShowMsg("支付编号不能为空，请修正后继续!");
                return false; 
            }
            if (this.edtZFBH.EditValue.ToString().Length > 50)
            {
                MsgBox.ShowMsg("支付编号的长度不能超过50，请修正后继续!");
                return false;
            }
            if (this.edtZFBHForThree.EditValue.ToString().Length > 50)
            {
                MsgBox.ShowMsg("支付编号的长度不能超过50，请修正后继续!");
                return false;
            }
            if (this.edtZFBHForFour.EditValue.ToString().Length > 50)
            {
                MsgBox.ShowMsg("支付编号的长度不能超过50，请修正后继续!");
                return false;
            }
            return this.method2();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (this.method3())
            {
                if (this._flag)
                {
                    this.method();
                }
                else
                {
                    this.method1();
                }
                base.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
        }
    }
}

